/*
A Pandemic Crossing the Border: The Impact of Covid-19 in the US on the Mexican Labor Market
Rojas and Yu, 2023

Replication file for Tables 4, 5, 6, and A5
*/

clear all
set more off

global data "COMPLETE YOUR DIRECTORY HERE \data"
global output "COMPLETE YOUR DIRECTORY HERE \output"

use "$data/cleaned_ind_daily.dta", clear

destring monthincomeifwork whrsifwork hrwageifwork, replace force

gen t=.
replace t=1 if qtr==12019
replace t=2 if qtr==22019
replace t=3 if qtr==32019
replace t=4 if qtr==42019
replace t=5 if qtr==12020
replace t=6 if qtr==32020
replace t=7 if qtr==42020

label define quarter_lab 1 "Q1-2019" 2 "Q2-2019" 3 "Q3-2019" 4 "Q4-2019" 5 "Q1-2020" 6 "Q3-2020" 7 "Q4-2020"
label values t quarter_lab

egen id2=group(id)
egen t2=group(int_date)

drop if telephone==1
drop if missing_emif==1

xtset id2 t

**Municipality size
gen loc_big=0
replace loc_big=1 if pop>200000

**Migration intensity (migrants_rate_q is the migrants rate quantile)
gen migrants_high=(migrants_rate_q=="q3" | migrants_rate_q=="q4" | migrants2010_rate_q=="q3" | migrants2010_rate_q=="q4")

keep if status1=="active"

**Restrict sample to positive migrants in 2020
drop if migrants==0


/*
Table 4
*/

**First state
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx remit_mpesos_r remit_mpesos_r_v2 [aw=weight], by(mx_mun t)
label var covid_exp_w "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
label var remit_mpesos_r_v2 "Remittance per Migrant (real mill. MXN)"
reghdfe remit_mpesos_r_v2 covid_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store first_w1
reghdfe remit_mpesos_r_v2 covid_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store first_w2
restore


/*
Table 5
*/

**OLS and IV results
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx remit_mpesos_r remit_mpesos_r_v2 [aw=weight], by(mx_mun t)
label var remit_mpesos_r_v2 "Remittance per Migrant (real mill. MXN)"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe whrs remit_mpesos_r_v2, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_w1_fe
reghdfe whrs remit_mpesos_r_v2 covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_w2_fe
xtset mx_mun t
tab t, gen(t_)
drop t_7
ivreghdfe whrs (remit_mpesos_r_v2=covid_exp_w), absorb(mx_mun t) cluster(mx_mun)
estimates store fs_w1
estadd scalar fs = round(e(widstat), 0.00001)
xtivreg whrs (remit_mpesos_r_v2=covid_exp_w) t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.001)
estimates restore fs_w1
estimates store fs_w1
estadd scalar arp = ar_p
ivreghdfe whrs (remit_mpesos_r_v2=covid_exp_w)  covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_w2
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=covid_exp_w) covid_exp_mx t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore fs_w2
estimates store fs_w2
estadd scalar arp = ar_p
restore

/*
Table 6
*/

**Males
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx remit_mpesos_r remit_mpesos_r_v2 if female==0 [aw=weight], by(mx_mun t)
label var remit_mpesos_r_v2 "Remittance per Migrant (real mill. MXN)"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
xtset mx_mun t
tab t, gen(t_)
drop t_7
ivreghdfe whrs (remit_mpesos_r_v2=covid_exp_w) , absorb(mx_mun t) cluster(mx_mun)
estimates store ma_w1
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=covid_exp_w) t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore ma_w1
estimates store ma_w1
estadd scalar arp = ar_p
ivreghdfe whrs (remit_mpesos_r_v2=covid_exp_w)  covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store ma_w2
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=covid_exp_w) covid_exp_mx t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore ma_w2
estimates store ma_w2
estadd scalar arp = ar_p
restore

**Females
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx remit_mpesos_r remit_mpesos_r_v2 if female==1 [aw=weight], by(mx_mun t)
label var remit_mpesos_r_v2 "Remittance per Migrant (real mill. MXN)"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
xtset mx_mun t
tab t, gen(t_)
drop t_7
ivreghdfe whrs (remit_mpesos_r_v2=covid_exp_w) , absorb(mx_mun t) cluster(mx_mun)
estimates store fe_w1
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=covid_exp_w) t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore fe_w1
estimates store fe_w1
estadd scalar arp = ar_p
ivreghdfe whrs (remit_mpesos_r_v2=covid_exp_w)  covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fe_w2
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=covid_exp_w) covid_exp_mx t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore fe_w2
estimates store fe_w2
estadd scalar arp = ar_p
restore

**Agriculture
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx remit_mpesos_r remit_mpesos_r_v2 if occ=="agriculture" [aw=weight], by(mx_mun t)
label var remit_mpesos_r_v2 "Remittance per Migrant (real mill. MXN)"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
xtset mx_mun t
tab t, gen(t_)
drop t_7
ivreghdfe whrs (remit_mpesos_r_v2=covid_exp_w) , absorb(mx_mun t) cluster(mx_mun)
estimates store ag_w1
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=covid_exp_w) t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore ag_w1
estimates store ag_w1
estadd scalar arp = ar_p
ivreghdfe whrs (remit_mpesos_r_v2=covid_exp_w)  covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store ag_w2
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=covid_exp_w) covid_exp_mx t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore ag_w2
estimates store ag_w2
estadd scalar arp = ar_p
restore

**Non-agriculture
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx remit_mpesos_r remit_mpesos_r_v2 if occ!="agriculture" [aw=weight], by(mx_mun t)
label var remit_mpesos_r_v2 "Remittance per Migrant (real mill. MXN)"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
xtset mx_mun t
tab t, gen(t_)
drop t_7
ivreghdfe whrs (remit_mpesos_r_v2=covid_exp_w) , absorb(mx_mun t) cluster(mx_mun)
estimates store na_w1
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=covid_exp_w) t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore na_w1
estimates store na_w1
estadd scalar arp = ar_p
ivreghdfe whrs (remit_mpesos_r_v2=covid_exp_w)  covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store na_w2
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=covid_exp_w) covid_exp_mx t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore na_w2
estimates store na_w2
estadd scalar arp = ar_p
restore

**High migration
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx remit_mpesos_r remit_mpesos_r_v2 if migrants_high==1 [aw=weight], by(mx_mun t)
label var remit_mpesos_r_v2 "Remittance per Migrant (real mill. MXN)"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
xtset mx_mun t
tab t, gen(t_)
drop t_7
ivreghdfe whrs (remit_mpesos_r_v2=covid_exp_w) , absorb(mx_mun t) cluster(mx_mun)
estimates store mh_w1
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=covid_exp_w) t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore mh_w1
estimates store mh_w1
estadd scalar arp = ar_p
ivreghdfe whrs (remit_mpesos_r_v2=covid_exp_w)  covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store mh_w2
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=covid_exp_w) covid_exp_mx t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore mh_w2
estimates store mh_w2
estadd scalar arp = ar_p
restore

**Low migration
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx remit_mpesos_r remit_mpesos_r_v2 if migrants_high==0 [aw=weight], by(mx_mun t)
label var remit_mpesos_r_v2 "Remittance per Migrant (real mill. MXN)"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
xtset mx_mun t
tab t, gen(t_)
drop t_7
ivreghdfe whrs (remit_mpesos_r_v2=covid_exp_w) , absorb(mx_mun t) cluster(mx_mun)
estimates store ml_w1
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=covid_exp_w) t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore ml_w1
estimates store ml_w1
estadd scalar arp = ar_p
ivreghdfe whrs (remit_mpesos_r_v2=covid_exp_w)  covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store ml_w2
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=covid_exp_w) covid_exp_mx t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore ml_w2
estimates store ml_w2
estadd scalar arp = ar_p
restore

/*
Table A5
*/

*IV is stringency indext
preserve
collapse (mean) hrwage whrs si_exp_w covid_exp_mx remit_mpesos_r remit_mpesos_r_v2 [aw=weight], by(mx_mun t)
label var remit_mpesos_r_v2 "Remittance per Migrant (real mill. MXN)"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
xtset mx_mun t
tab t, gen(t_)
drop t_7
ivreghdfe whrs (remit_mpesos_r_v2=si_exp_w) , absorb(mx_mun t) cluster(mx_mun)
estimates store fs_si_w1
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=si_exp_w) t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore fs_si_w1
estimates store fs_si_w1
estadd scalar arp = ar_p
ivreghdfe whrs (remit_mpesos_r_v2=si_exp_w)  covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_si_w2
estadd scalar fs = round(e(widstat), 0.001)
xtivreg whrs (remit_mpesos_r_v2=si_exp_w) covid_exp_mx t_*, fe vce(cluster mx_mun)
weakiv
scalar ar_p= round(e(ar_p), 0.00001)
estimates restore fs_si_w2
estimates store fs_si_w2
estadd scalar arp = ar_p
restore

			
**Create tables
cd "$output"

label var remit_mpesos_r_v2 "Remittance per Migrant (real mill. MXN)"
label var covid_exp_w "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"

**Table 4
esttab first_w1 first_w2 using "table_4.tex", cells(b(star fmt(a3))  se(par(`"("'`")"')) )  starlevels(* .1 ** .05 *** .01) stats(N, fmt(%9.0gc)) label replace ///
	title("Effects of Covid-19 Exposure on Remittance") ///
	note("Note: standard errors clustered at the municipality level in parentheses.") ///
	addnote("Significance codes are * 0.1, ** 0.05, *** 0.01") ///
	mtitle("\shortstack{All\\Remittance per Migrant \\ (real mill. MXN)}" "\shortstack{All\\ Remittance per Migrant \\ (real mill. MXN)}")
	
**Table 5
esttab fs_w1_fe fs_w2_fe fs_w1 fs_w2 using "table_5.tex", cells(b(star fmt(a3))  se(par(`"("'`")"')) )  starlevels(* .1 ** .05 *** .01) stats(N fs arp, fmt(%9.0gc)) label replace ///
	title("Effects of Remittance on worked hours") ///
	note("Note: standard errors clustered at the municipality level in parentheses.") ///
	addnote("Significance codes are * 0.1, ** 0.05, *** 0.01") ///
	mtitle("\shortstack{All\\Hours}" "\shortstack{All\\ Hours}""\shortstack{All\\Hours}" "\shortstack{All\\ Hours}")
	
**Table 6
**Gender
esttab ma_w1 ma_w2 fe_w1 fe_w2 using "table_6_a.tex", cells(b(star fmt(a3))  se(par(`"("'`")"')) )  starlevels(* .1 ** .05 *** .01) stats(N fs arp, fmt(%9.0gc)) label replace ///
	title("Effects of Remittance on worked hours by gender") ///
	note("Note: standard errors clustered at the municipality level in parentheses. Significance codes are * 0.1, ** 0.05, *** 0.01") ///
	mtitle( "\shortstack{Males\\Hours}" "\shortstack{Males\\Hours}" "\shortstack{Females\\Hours}" "\shortstack{Females\\Hours}")
	
**Occupations
esttab ag_w1 ag_w2 na_w1 na_w2 using "table_6_b.tex", cells(b(star fmt(a3))  se(par(`"("'`")"')) )  starlevels(* .1 ** .05 *** .01) stats(N fs arp, fmt(%9.0gc)) label replace ///
	title("Effects of Remittance on worked hours by type of occupation") ///
	note("Note: standard errors clustered at the municipality level in parentheses. Significance codes are * 0.1, ** 0.05, *** 0.01") ///
	mtitle("\shortstack{Agriculture\\Hours}" "\shortstack{Agriculture\\Hours}" "\shortstack{Non-agriculture\\Hours}" "\shortstack{Non-agriculture\\Hours}") 
	
**Migration intensity
esttab mh_w1 mh_w2 ml_w1 ml_w2 using "table_6_c.tex", cells(b(star fmt(a3))  se(par(`"("'`")"')) )  starlevels(* .1 ** .05 *** .01) stats(N fs arp, fmt(%9.0gc)) label replace ///
	title("Effects of Remittance on worked hours by migration intensity") ///
	note("Note: standard errors clustered at the municipality level in parentheses. Significance codes are * 0.1, ** 0.05, *** 0.01") ///
	mtitle("\shortstack{High migration\\Hours}" "\shortstack{High migration\\Hours}" "\shortstack{Low migration\\Hours}" "\shortstack{Low migration\\Hours}") 	

**Table A5
esttab fs_si_w1 fs_si_w2 using "table_A5.tex", cells(b(star fmt(a3))  se(par(`"("'`")"')) )  starlevels(* .1 ** .05 *** .01) stats(N fs arp, fmt(%9.0gc)) label replace ///
	title("Effects of Remittance on worked hours") ///
	note("Note: standard errors clustered at the municipality level in parentheses.") ///
	addnote("Significance codes are * 0.1, ** 0.05, *** 0.01") ///
	mtitle("\shortstack{All\\Hours}" "\shortstack{All\\ Hours}")
	
	
	
	
	
	
	
